home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / reader_requests / xgtool / xgtoolset / source / xgreset / xgreset.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  12KB  |  507 lines

  1. /*
  2.     RSXG-Reset
  3.  
  4.     A Bars and Pipes tool for reseting the DB50-XG-board from  YAMAHA.  
  5.     Use it together with the other XG ... .ptool ' s -
  6.  
  7.     © 1998 Alfred & Matthias Faust
  8.  
  9.     The Tool-code based farly on the code for the "pc3.ptool" from  Richard
  10.     Hagen. That's a very good tool,  many thanks to him.
  11.  
  12.     Richard has written his tool with DICE. We altered the code to  compile
  13.     with  the  free  GNU-C-compiler  of  the  ADE.  Especially the part was
  14.     transcripted,   that  place  the  imagedate  in  chip-RAM,   because  the
  15.     GNU-C-compiler  hasn't as yet a keyword like DICE or other compiler for
  16.     this.
  17.  
  18.     This code is released  into  the  Public  Domain,   and  may  be  freely
  19.     distributed in its original form.
  20.  
  21.     It is supplied ``as is'',  and comes with no warranty. This program code
  22.     was  released  because it might be useful as a starting point for other
  23.     programmers. However,  if any damage arises from its use,   the  original
  24.     author and we will not be held liable.
  25.  
  26.     You are free to use and modify  this  code  to  your  heart's  content,
  27.     provided you acknowledge me as the original author in any code that you
  28.     might distribute which is based largely on this code.
  29.  
  30.     I acknowledge that the design of this tool is  influenced  strongly  by
  31.     the  example code supplied with the Rules for Tools package. However,  I
  32.     have made substantial contributions of my own.
  33.  
  34.     Matthias & Alfred Faust
  35.     j.k.dax@t-online.de
  36.  
  37.     History:
  38.     1.0 (2.12.1998)
  39.     Initial Release.
  40.  
  41.  
  42.     
  43. */
  44.  
  45. #include "bars.h"
  46. #include "myheader.h"  // from R.Hagen
  47. #include <libraries/dos.h>
  48. #include <proto/exec.h>
  49. #include <proto/intuition.h>
  50. #include <proto/graphics.h>
  51. #include <exec/memory.h>
  52. #include <exec/execbase.h>
  53. #include <exec/types.h>
  54. #include <string.h>
  55. #include <intuition/intuition.h>
  56.  
  57. char version[]="\0$VER:RSXG-Edit 1.0 (2.12.98) © Alfred & Matthias Faust";
  58.  
  59. #define RSXG_NAME "XG-Reset"
  60.  
  61. #define RSXG_ID          0x52535847
  62. #define RSXG_TYPE        (TOOL_NORMAL | TOOL_NOTPAD)
  63.  
  64.  
  65.  
  66. /* the litte Icon in the pipeline */
  67.  
  68. UWORD *RSXG=NULL;
  69.  
  70. static UWORD RSXGdata[] =
  71. {
  72.     /* Plane 0 */
  73.         0x7FFF,0xFE00,
  74.         0x7FFF,0xFE00,
  75.         0x6000,0x0600,
  76.         0x79FB,0x9E00,
  77.         0x7E34,0x7E00,
  78.         0x7BC3,0xDE00,
  79.         0x7BF3,0xDE00,
  80.         0x7A7C,0x5E00,
  81.         0x79EF,0x9E00,
  82.         0x6600,0x6600,
  83.         0x7FFF,0xFE00,
  84.         0x7FFF,0xFE00,
  85.     /* Plane 1 */
  86.         0x0000,0x0200,
  87.         0x0000,0x0600,
  88.         0x1800,0x1E00,
  89.         0x07FF,0xE600,
  90.         0x99FF,0x9F00,
  91.         0x9A3C,0x5F00,
  92.         0x9A4F,0xDF00,
  93.         0x9B83,0xDF00,
  94.         0x1E6E,0x7E00,
  95.         0x1800,0x1E00,
  96.         0x1FFF,0xFE00,
  97.         0x3FFF,0xFE00,
  98.     /* Plane 2 */
  99.         0x0000,0x0000,
  100.         0x0000,0x0000,
  101.         0x1800,0x1800,
  102.         0x1FFF,0xF800,
  103.         0x1FFF,0xF800,
  104.         0x1BFF,0xD800,
  105.         0x1BFF,0xD800,
  106.         0x1BFF,0xD800,
  107.         0x1FFF,0xF800,
  108.         0x1E00,0x7800,
  109.         0x0000,0x0000,
  110.         0x0000,0x0000
  111. };
  112.  
  113. static struct Image RSXG_image = {
  114.   0, 0,
  115.   24 ,  12 ,  3 ,
  116.   NULL,
  117.   0x1f, 0x00,
  118.   NULL
  119. };
  120.  
  121.  
  122. //Definitions of the gadgets
  123.  
  124. #define WRITE_GADGET 1
  125. #define SEND_GADGET  2
  126.  
  127. //all the gadgets
  128.  
  129.  
  130. /* Write to track gadget. */
  131.  
  132. struct IntuiText RSXGIText1 = {
  133.   4, 0, JAM1, 4, 2, NULL, "Write", NULL
  134.   };
  135.  
  136. struct Gadget RSXGGadget1 = {
  137.   NULL, 60, 18, 45, 11, GFLG_GADGHBOX|GFLG_GADGHIMAGE, RELVERIFY|GADGIMMEDIATE, BOOLGADGET,
  138.   NULL, NULL, &RSXGIText1, NULL, NULL, WRITE_GADGET, NULL
  139.   };
  140.  
  141. /* Test gadget. */
  142.  
  143. struct IntuiText RSXGIText2 = {
  144.   4, 0, JAM1, 4, 2, NULL, "Send", NULL
  145.   };
  146.  
  147. struct Gadget RSXGGadget2 = {
  148.   &RSXGGadget1, 11, 18, 40, 11, GFLG_GADGHBOX|GFLG_GADGHIMAGE, RELVERIFY|GADGIMMEDIATE, BOOLGADGET,
  149.   NULL, NULL, &RSXGIText2, NULL, NULL, SEND_GADGET, NULL
  150.   };                                        
  151.  
  152. //the mainwindowstructure
  153.  
  154. struct NewWindow RSXGNewWindowStructure1 = {
  155.   5, 15,
  156.   120,  35,
  157.   0, 6,
  158.   GADGETDOWN|GADGETUP|CLOSEWINDOW,
  159.   WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|ACTIVATE|NOCAREREFRESH,
  160.   &RSXGGadget2,
  161.   NULL,
  162.   "XG Reset",
  163.   NULL,
  164.   NULL,
  165.   5, 5,
  166.   -1, -1,
  167.   CUSTOMSCREEN
  168. };
  169.  
  170. extern struct ExecBase * SysBase;
  171.  
  172. struct RSXGTool {
  173.   struct Tool tool;
  174.  
  175.   char *resetsysex;
  176.   char *xgonsysex;
  177. };
  178.  
  179. static struct ToolMaster master;
  180.  
  181. extern struct Functions *functions;
  182.  
  183. //initial values
  184. static void RSXG_tool_init(struct RSXGTool *tool)
  185. {
  186.   tool->tool.touched = TOUCH_INIT;
  187.  
  188.   tool->resetsysex = "F07E7F0901F7";
  189.   tool->xgonsysex  = "F043104C00007E00F7";
  190. }
  191.  
  192.  
  193. static struct Event * __saveds processeventcode(struct Event *event)
  194. {
  195.   event->tool = event->tool->next;
  196.   return(event);
  197. }
  198.  
  199. static BOOL lock_clip(struct Clip *clip)
  200. {
  201.   Forbid();
  202.   if (clip->locked == 0)
  203.     {
  204.       clip->locked = 1;
  205.       Permit();
  206.       return TRUE;
  207.     }
  208.   else
  209.     {
  210.       Permit();
  211.       return FALSE;
  212.     }
  213. }
  214.  
  215. static void unlock_clip(struct Clip *clip)
  216. {
  217.   clip->locked = 0;
  218. }
  219.  
  220. //writes sysex into track
  221.  
  222. static void __saveds insertsysex(char *buff, short size, struct RSXGTool *tool,  const LONG time)
  223. {
  224.     struct StringEvent *event;
  225.     struct String *string;
  226.  
  227.     event = (struct StringEvent *) (*functions->allocevent)();
  228.      if (event)
  229.       {
  230.     string = (struct String *) (*functions->myalloc)(size + 3, MEMF_CLEAR);
  231.     if (string)
  232.     {
  233.         event->next = NULL;
  234.         event->string = string;
  235.         memcpy(string->string,  buff,  size);
  236.         string->length = size + 2;
  237.         event->type = EVENT_SYSX;
  238.         event->status = MIDI_SYSX;
  239.         event->time = time;
  240.         event->tool = tool->tool.next;
  241.         if (lock_clip(&(tool->tool.track->clip)))
  242.           {
  243.         event->next = (struct StringEvent *)tool->tool.track->clip.events.first;
  244.         tool->tool.track->clip.events.first = (struct Event *)(*functions->sorteventlist)(event);
  245.         unlock_clip(&(tool->tool.track->clip));
  246.           }
  247.         else
  248.           {
  249.         (*functions->freelist)(event);
  250.           }
  251.  
  252.     }
  253.       }
  254.  
  255. }
  256.  
  257. // the routine for sending SysEx-data thru the pipeline
  258.  
  259. static void __saveds sendsysex(char *buff,  short size,  struct RSXGTool *tool,  const LONG time)
  260. {
  261.     struct StringEvent *event;
  262.     struct String *string;
  263.  
  264.     event = (struct StringEvent *) (*functions->allocevent)();
  265.      if (event)
  266.       {
  267.     string = (struct String *) (*functions->myalloc)(size + 3, MEMF_CLEAR);
  268.     if (string)
  269.     {
  270.         event->string = string;
  271.         memcpy(string->string,  buff,  size);
  272.         string->length = size + 2;
  273.         event->type = EVENT_SYSX;
  274.         event->status = MIDI_SYSX;
  275.         event->time = time;
  276.         event->tool = tool->tool.next;
  277.         WaitTOF();
  278.         (*functions->qevent)(event);
  279.         (*functions->myfree)(string);
  280.     }
  281.       }
  282.  
  283. }
  284.  
  285. static void __saveds trans_hx(struct RSXGTool *tool,  const LONG time,  char *sysx, int sw)
  286. {
  287.   LONG size;
  288.   int a,  b,  x,  y ;
  289.   UBYTE abuf[32];
  290.   char chr[4];
  291.   char *zbuf = NULL;
  292.   struct Event *sysx_event;
  293.  
  294.     //translate sysexbuffer into hex
  295.  
  296.   size = strlen(sysx);
  297.   zbuf = (char *)AllocVec(size+2,  MEMF_ANY|MEMF_CLEAR);
  298.   if(zbuf)
  299.      {
  300.        CopyMem(sysx,  zbuf,  size);
  301.        b=0;
  302.        for(a=0;a < size;a=a+2)
  303.        {
  304.          chr[0] = zbuf[a];
  305.          chr[1] = zbuf[a+1];
  306.  
  307.          x=(isdigit((int)chr[0]))? (chr[0]&0xF) : ((chr[0]&0xDF)-55); //this is the
  308.          y=(isdigit((int)chr[1]))? (chr[1]&0xF) : ((chr[1]&0xDF)-55); //the translate-core
  309.  
  310.          abuf[b++] = ((x<<4)|y);   //writing with bitmanipulation
  311.         }
  312.       //send hexdata
  313.  
  314.        FreeVec(zbuf);
  315.        if (sw == 0) sendsysex(abuf,  b,  tool,  time);
  316.        if (sw == 1) insertsysex(abuf,  b,  tool,  time);
  317.  
  318.      }
  319.  
  320. }
  321.  
  322. //to hear the changes
  323.  
  324. static void send_note(struct RSXGTool *tool,  const long time,  const UBYTE note)
  325. {
  326.   struct NoteEvent *on_event = (struct NoteEvent *)(*functions->allocevent)();
  327.   if (on_event)
  328.     {
  329.       struct NoteEvent *off_event = (struct NoteEvent *)(*functions->allocevent)();
  330.       if (off_event)
  331.     {
  332.       on_event->next  =
  333.       off_event->next = NULL;
  334.  
  335.       on_event->type  =
  336.       off_event->type = EVENT_VOICE;
  337.  
  338.       on_event->status  = MIDI_NOTEON;
  339.       off_event->status = MIDI_NOTEOFF;
  340.  
  341.       on_event->value  =
  342.       off_event->value = note;
  343.  
  344.       on_event->velocity = 80;
  345.  
  346.       on_event->time  = time;
  347.       off_event->time = time + 200;
  348.  
  349.       on_event->tool  =
  350.       off_event->tool = tool->tool.next;
  351.  
  352.       (*functions->qevent)(on_event);
  353.       (*functions->qevent)(off_event);
  354.     }
  355.       else
  356.     {
  357.       (*functions->freeevent)(on_event);
  358.     }
  359.     }
  360. }
  361.  
  362.  
  363. //the center off all
  364.  
  365. static void __saveds edittoolcode(struct RSXGTool *tool)
  366. {
  367.   struct IntuiMessage *message;
  368.   struct Window *window;
  369.   LONG class,  code,  time;
  370.   struct Gadget *gadget;
  371.   struct NewWindow *newwindow;
  372.   int i;
  373.  
  374.  
  375.   RSXGNewWindowStructure1.Screen = functions->screen;
  376.  
  377.   if (tool->tool.touched & TOUCH_EDIT)
  378.     {
  379.       RSXGNewWindowStructure1.LeftEdge = tool->tool.left;
  380.       RSXGNewWindowStructure1.TopEdge = tool->tool.top;
  381.       RSXGNewWindowStructure1.Width = tool->tool.width;
  382.       RSXGNewWindowStructure1.Height = tool->tool.height;
  383.     }
  384.   
  385.   if (!tool->tool.touched)
  386.     {
  387.       RSXG_tool_init(tool);
  388.     }
  389.  
  390.   newwindow = (struct NewWindow *) (*functions->DupeNewWindow)(&RSXGNewWindowStructure1);
  391.   if (!newwindow)
  392.     {
  393.       return;
  394.     }
  395.   newwindow->Title = 0;
  396.   newwindow->Flags |= BORDERLESS;
  397.   newwindow->Flags &= ~0xF;
  398.   newwindow->BlockPen = 0;
  399.   newwindow->DetailPen = 0;
  400.  
  401.   window = (struct Window *) (*functions->FlashyOpenWindow)(newwindow);
  402.   if (!window)
  403.     {
  404.       return;
  405.     }
  406.   tool->tool.window = window;
  407.   (*functions->EmbossWindowOn)(window, WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG,  RSXG_NAME, (short)-1, (short)-1, 0, 0);
  408.  
  409.  
  410.   (*functions->EmbossOn)(window,  WRITE_GADGET,  1);
  411.   (*functions->EmbossOn)(window,  SEND_GADGET,  1);
  412.   send_note(tool,  functions->timenow,  60);
  413.  
  414.   for (;;)
  415.     {
  416.  
  417.       message = (struct IntuiMessage *) (*functions->GetIntuiMessage)(window);
  418.       class = message->Class;
  419.       code = message->Code;
  420.       gadget = (struct Gadget *) message->IAddress;
  421.       class = (*functions->SystemGadgets)(window, class, gadget, code);
  422.  
  423.       ReplyMsg((struct Message *)message);
  424.  
  425.  
  426.       if (class == CLOSEWINDOW)
  427.     {
  428.       break;
  429.     }
  430.       else if (class == GADGETDOWN)
  431.     {
  432.       const short id = gadget->GadgetID;
  433.       switch (id)
  434.         {
  435.         case WRITE_GADGET:
  436.           if ((*functions->areyousure)("write to track ?"))
  437.           {
  438.           if (!functions->running)
  439.               {
  440.                if (tool->resetsysex != NULL) trans_hx(tool,  1,  tool->resetsysex, 1);
  441.                if (tool->xgonsysex != NULL) trans_hx(tool,  384,  tool->xgonsysex, 1);
  442.               }
  443.           }
  444.         send_note(tool,  time,  60);
  445.           break;
  446.         case SEND_GADGET:
  447.           {
  448.         long time = functions->timenow;
  449.         trans_hx(tool, functions->timenow, tool->resetsysex, 0);
  450.         trans_hx(tool, functions->timenow, tool->resetsysex, 0);
  451.         send_note(tool,  functions->timenow,  60);
  452.           }
  453.         default:
  454.         break;
  455.         }
  456.       }
  457.       else if (class == GADGETUP)
  458.     {
  459.       /* Nothing. */
  460.     }
  461.     }
  462.   tool->tool.window = 0;
  463.   tool->tool.left = window->LeftEdge;
  464.   tool->tool.top = window->TopEdge;
  465.   tool->tool.width = window->Width;
  466.   tool->tool.height = window->Height;
  467.   tool->tool.touched = TOUCH_INIT | TOUCH_EDIT;
  468.  
  469.  
  470.   (*functions->EmbossOff)(window,  WRITE_GADGET);
  471.   (*functions->EmbossOff)(window,  SEND_GADGET);
  472.   
  473.   (*functions->EmbossWindowOff)(window);
  474.   (*functions->FlashyCloseWindow)(window);
  475.   (*functions->DeleteNewWindow)(newwindow);
  476. }
  477.  
  478. //to leave the tool correct
  479.  
  480. void __saveds removetool()
  481. {
  482. FreeVec(RSXG);
  483. }
  484.  
  485. //start of the tool
  486.  
  487. struct ToolMaster * inittoolmaster()
  488. {
  489.   int i = 0,  bank = 0;
  490.  
  491.   if (!(RSXG=(UWORD *)AllocVec(sizeof (RSXGdata), MEMF_CHIP|MEMF_CLEAR))) return(NULL);
  492.   CopyMem(&RSXGdata, RSXG,  sizeof (RSXGdata));
  493.  
  494.  
  495.   RSXG_image.ImageData = RSXG;
  496.   memset((char *)&master, 0, sizeof(struct ToolMaster));
  497.   master.toolid = RSXG_ID;
  498.   master.image = &RSXG_image;
  499.   strcpy(master.name,  RSXG_NAME);
  500.   master.edittool = edittoolcode;
  501.   master.processevent = processeventcode;
  502.   master.tooltype = RSXG_TYPE;
  503.   master.toolsize = sizeof(struct RSXGTool);
  504.   master.removetool = removetool;
  505.   return(&master);
  506. }
  507.